<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">商品管理</a></el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="margin-top: 15px">
      <el-row>
        <el-col>
          <el-button type="primary" @click="openAddCateDialog">♔添加商品</el-button>
        </el-col>
      </el-row>
      <tree-table style="margin-top: 15px"
      :data= "cateList" :columns= "columns"
      :selection-type="false" :expand-type="false" show-index index-text="#" border>
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" v-if="scope.row.cat_deleted === false"
          style="color:green"></i>
          <i class="el-icon-error" style="color:red" v-else></i>
        </template>
        <!-- 等级模板列 -->
        <template slot="rank" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success">二级</el-tag>
          <el-tag v-else type="warning">三级</el-tag>
        </template>
        <template slot="opt" slot-scope="scope">
          <el-button type="primary" @click="openEditGoodDialog(scope.row.cat_id)">编辑</el-button>
          <el-button type="danger" @click="deleteGood(scope.row.cat_id)">删除</el-button>
        </template>
      </tree-table>
    </el-card>
    <!-- 分页 -->
    <el-pagination
          style="margin-top: 20px"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="querInfo.pagenum"
          :page-sizes="[5, 10, 15, 20, 25, 30]"
          :page-size="querInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total= "total">
    </el-pagination>
    <!-- 编辑商品对话框 -->
    <el-dialog
      title="编辑商品" :visible.sync="editDialogVisible" width="30%">
      <el-form
        :model="editCateForm"
        ref="editCateFormRef"
        label-width="70px">
       <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="editCateForm.cat_name"></el-input>
       </el-form-item>
      </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="editDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="editCate">确 定</el-button>
        </span>
    </el-dialog>
    <!-- 添加商品对话框 -->
    <el-dialog  title="添加商品" :visible.sync="addDialogVisible" width="30%" @close="addCateDialogClose">
      <el-form
        :model="addCateForm"
        ref="addCateFormRef"
        label-width="70px">
       <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
       </el-form-item>
       <el-form-item label="父级分类">
         <el-cascader expand-trigger="hover" :options="parentCateList" :props="cascaderProps"
         v-model="selectedKeys" @change="parentChange" clearable separator=' / '>
         </el-cascader>
       </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      checked: true,
      unchecked: true,
      querInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      cateList: [],
      editCateForm: {},
      // 添加的商品内容
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        cat_level: 0
      },
      // 父级分类列表
      parentCateList: [],
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children',
        checkStrictly: true
      },
      selectedKeys: [],
      total: 0,
      columns: [
        { label: '分类名称', prop: 'cat_name' },
        { label: '是否有效', type: 'template', template: 'isok' },
        { label: '排序', type: 'template', template: 'rank' },
        { label: '操作', type: 'template', template: 'opt' }
      ],
      editDialogVisible: false,
      addDialogVisible: false
    }
  },
  created() {
    this.getCateList()
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get('categories', { params: this.querInfo })
      if (res.meta.status !== 200) {
        return this.$meesage.error('获取商品列表失败')
      }
      this.cateList = res.data.result
      this.total = res.data.total
    },
    // 监听页数变化
    handleSizeChange(newSize) {
      this.querInfo.pagesize = newSize
      this.getCateList()
    },
    // 监听每页条数变化
    handleCurrentChange(newPage) {
      this.querInfo.pagenum = newPage
      this.getCateList()
    },
    deleteGood(id) {
      this.$confirm('确认删除？')
        .then(async () => {
          const { data: res } = await this.$http.delete('categories/' + id)
          if (res.meta.status !== 200) {
            return this.$message.error('删除失败')
          }
          this.getCateList()
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    async openEditGoodDialog(id) {
      this.editDialogVisible = true
      const { data: res } = await this.$http.get('categories/' + id)
      if (res.meta.status !== 200) {
        return this.$message.error('删除失败')
      }
      this.editCateForm = res.data
    },
    async editCate() {
      const { data: res } = await this.$http.put('categories/' + this.editCateForm.cat_id, { cat_name: this.editCateForm.cat_name })
      if (res.meta.status !== 200) {
        return this.$message.error('修改失败')
      }
      this.getCateList()
      this.editDialogVisible = false
    },
    // 添加商品
    openAddCateDialog() {
      this.getParentCateList()
      this.addDialogVisible = true
    },
    async getParentCateList() {
      const { data: res } = await this.$http.get('categories', {
        params: { type: 2 }
      })
      if (res.meta.status !== 200) {
        return this.$meesage.error('获取父级分类失败')
      }
      this.parentCateList = res.data
    },
    parentChange() {
    },
    async addCate() {
      if (this.selectedKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length - 1]
        // 为当前分类等级赋值
        this.addCateForm.cat_level = this.selectedKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        // 为当前分类等级赋值
        this.addCateForm.cat_level = 0
      }
      const { data: res } = await this.$http.post('categories', this.addCateForm)
      if (res.meta.status !== 201) {
        return this.$message.error('添加商品失败')
      }
      this.getCateList()
      this.addDialogVisible = false
    },
    addCateDialogClose() {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    }
  }
}
</script>

<style lang="less" scoped>
</style>
